/*
*ADSprite
*chenjie
*2012-5-6
*/
package com.player
{
	import com.data.Data;
	import com.data.PlayModel;
	import com.event.PlayerEvent;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.system.Capabilities;
	import flash.utils.Timer;
	
	public class ADSprite extends Sprite
	{
		private var loader:Loader=new Loader()
		private var timer:Timer
		private var curPlayModel:PlayModel	
		public function ADSprite()
		{
			super();
			addChild(loader)
			addEventListener(MouseEvent.CLICK,doClick)
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadComplete)
			this.visible=false
		}
		
		public function doClick(e:MouseEvent):void{
			if (Capabilities.playerType == 'ActiveX')
			{
				ExternalInterface.call("window.open",curPlayModel.adUrl);
			}
			else
			{
				navigateToURL(new URLRequest(curPlayModel.adUrl),"_blank");
			}
		}
		
		public function changeWH(w:int,h:int):void{
			graphics.clear()
			graphics.beginFill(0x000000,1)
			graphics.drawRect(0,0,w,h)
			graphics.endFill()
				
			scrollRect=new Rectangle(0,0,w,h)	
		}
		
		public function setPlayModel(playModel:PlayModel):void{
			curPlayModel=playModel
			this.visible=false
			loader.load(new URLRequest(playModel.url))
		}
		
		public function loadComplete(e:Event):void{
			this.visible=true
//			timer.delay=1
//			timer.currentCount=int(curPlayModel.time)	
			timer=new Timer(1000,int(curPlayModel.time))	
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,timerComplete)
			timer.addEventListener(TimerEvent.TIMER,doTimer)
			timer.start()
		}
		
		public function doTimer(e:TimerEvent):void{
			dispatchEvent(new PlayerEvent(PlayerEvent.TIME))
		}
		
		
		public function timerComplete(e:TimerEvent):void{
			timer.removeEventListener(TimerEvent.TIMER_COMPLETE,timerComplete)
			timer.removeEventListener(TimerEvent.TIMER,doTimer)
			timer.stop()
			timer=null
			this.visible=false	
		    dispatchEvent(new Event(Event.COMPLETE))
		}
		
		
		
	}
}